package com.gc.ykqx.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gc.ykqx.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by clem on 2017/3/20.
 */
public class ChooseListAdapter extends BaseAdapter {
    private Context mContext;
    private LayoutInflater inflater;
    private List<String> names;
    private int selectedPosition = -1;

    public ChooseListAdapter(Context context, List<String> names) {
        this.mContext = context;
        this.names = names;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return names.size();
    }

    @Override
    public Object getItem(int position) {
        return names.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.choose_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (selectedPosition == position) {
            holder.colorLayout.setBackgroundColor(Color.parseColor("#3baeda"));
        } else {
            holder.colorLayout.setBackgroundColor(Color.parseColor("#4FC0E8"));
        }
        holder.textView.setText(names.get(position));
        return convertView;
    }

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    static class ViewHolder {
        @BindView(R.id.tv_name)
        TextView textView;
        @BindView(R.id.item_layout)
        LinearLayout colorLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
